package com.mytest.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorTest {

    public static void main(String[] args) {
        for(int j = 0;j <10000; j++){
            ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 2, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<>(5));
            CountDownLatch countDownLatch = new CountDownLatch(15);
            for(int i=0;i<15;i++){
                System.out.println("i==========================================="+i);
                int finalJ = j;
                int finalI = i;
                executor.execute(new Runnable(){

                    @Override
                    public void run() {
                        System.out.println("第"+ finalJ +"轮，正在执行task "+ finalI);
//                        if (finalJ  == 499){
//                            try {
//                                Thread.sleep(1000);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                        }
                        try {
                            Thread.sleep(5);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        System.out.println(Thread.currentThread().getName());
                        System.out.println("第"+ finalJ +"轮，"+ finalI +"执行完毕");
                        countDownLatch.countDown();
                    }
                });

//                System.out.println("executor.getTaskCount()========>" + executor.getTaskCount());
//                System.out.println("executor.getActiveCount()========>" + executor.getActiveCount());
//                System.out.println("线程池中线程数目："+executor.getPoolSize()+"，队列中等待执行的任务数目："+
//                        executor.getQueue().size()+"，已执行玩别的任务数目："+executor.getCompletedTaskCount());

            }

            try {
                countDownLatch.await(10, TimeUnit.SECONDS);
                executor.shutdown();
                System.out.println("第"+ j +"轮*********");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
